#region Imported Namespaces



using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;



#endregion


namespace WtfManager.UIManager
{
  ///<summary>
  /// Gets and stores information on UI assembly.
  ///</summary>
  public class UIAssemblyInfo
  {
    #region Fields



    private readonly Assembly _assembly;
    private readonly List<UIControlInfo> _contentContainerTypes = new List<UIControlInfo>();
    private readonly List<UIControlInfo> _startupWindowTypes = new List<UIControlInfo>();
    private readonly List<UIControlInfo> _panelControls = new List<UIControlInfo>();


    #endregion


    #region Constructors/Finalizers



    ///<summary>
    /// UIAssemblyInfo default constructor.
    ///</summary>
    ///<param name="assembly">An <see cref="System.Reflection.Assembly"/> containing user interface.</param>
    public UIAssemblyInfo(Assembly assembly)
    {
      _assembly = assembly;

      ProcessAssemblyTypes();
    }



    #endregion


    #region Properties/Indexers



    ///<summary>
    /// Gets an <see cref="System.Reflection.Assembly"/> containing user interface.
    ///</summary>
    public Assembly Assembly
    {
      get
      {
        return _assembly;
      }
    }


    ///<summary>
    /// Gets the type of content container control.
    ///</summary>
    public List<UIControlInfo> ContentContainerTypes
    {
      get
      {
        return _contentContainerTypes;
      }
    }


    ///<summary>
    /// Gets the list of startup widow types.
    ///</summary>
    public List<UIControlInfo> StartupWindowTypes
    {
      get
      {
        return _startupWindowTypes;
      }
    }

    public List<UIControlInfo> PanelControls
    {
      get
      {
        return _panelControls;
      }
    }


    #endregion


    #region Methods



    private void ProcessAssemblyTypes()
    {
      Type[] types = _assembly.GetTypes();

      foreach (Type type in types)
      {
        var uiControlInfo = new UIControlInfo(this, type);

        if (uiControlInfo.IsStartupWindow)
          _startupWindowTypes.Add(uiControlInfo);

        if (uiControlInfo.IsContentContainer)
          _contentContainerTypes.Add(uiControlInfo);

        if (uiControlInfo.IsPanelControl)
          _panelControls.Add(uiControlInfo);
      }
    }


    /// <exception cref="ArgumentNullException"><c>type</c> is null.</exception>
    public object CreateInstance(Type type)
    {
      if (type == null)
        throw new ArgumentNullException("type");

      object instance = null;

      try
      {
        instance = _assembly.CreateInstance(type.FullName);
      }
      catch (Exception)
      {
        // TODO: Log this.
      }

      return instance;
    }



    #endregion
  }
}